/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Maps;
import com.zhuoyue.zhtj.common.ServerResponse;
import com.zhuoyue.zhtj.entity.Kckqjl;
import com.zhuoyue.zhtj.entity.Kkcdtj;
import com.zhuoyue.zhtj.entity.TCourse;
import com.zhuoyue.zhtj.service.KckqjlService;
import com.zhuoyue.zhtj.service.KkcdtjService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Kqjl;
import com.zhuoyue.zhtj.service.KqjlService;

import java.util.List;
import java.util.Map;

/**
 * 考勤记录Controller
 * @author hss
 * @version 2017-09-26
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/kqjl")
public class KqjlController extends BaseController {

	@Autowired
	private KqjlService kqjlService;
	@Autowired
	private KkcdtjService kkcdtjService;
	@Autowired
	private KckqjlService kckqjlService;
	
	@ModelAttribute
	public Kqjl get(@RequestParam(required=false) String id) {
		Kqjl entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kqjlService.get(id);
		}
		if (entity == null){
			entity = new Kqjl();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:kqjl:view")
	@RequestMapping(value = {"list", ""})
	public String list(Kqjl kqjl, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Kqjl> page = kqjlService.findPage(new Page<Kqjl>(request, response), kqjl); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/kqjlList";
	}

	@RequiresPermissions("zhtj:kqjl:view")
	@RequestMapping(value = "form")
	public String form(Kqjl kqjl, Model model) {
		model.addAttribute("kqjl", kqjl);
		return "zhuoyue/zhtj/kqjlForm";
	}

	@RequiresPermissions("zhtj:kqjl:edit")
	@RequestMapping(value = "save")
	public String save(Kqjl kqjl, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kqjl)){
			return form(kqjl, model);
		}
		kqjlService.save(kqjl);
		addMessage(redirectAttributes, "保存考勤记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/kqjl/?repage";
	}
	
	@RequiresPermissions("zhtj:kqjl:edit")
	@RequestMapping(value = "delete")
	public String delete(Kqjl kqjl, RedirectAttributes redirectAttributes) {
		kqjlService.delete(kqjl);
		addMessage(redirectAttributes, "删除考勤记录成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/kqjl/?repage";
	}

	@RequestMapping("getCourseBykqjl")
	public String getCourseBykqjl(Kqjl kqjl, Kkcdtj kkcdtj, Kckqjl kckqjl, HttpServletResponse response, HttpServletRequest request){
//		if(kqjlService.getCourseBykqjl(kqjl)){
//			Map<String,Object> map= Maps.newHashMap();
//			List<Kkcdtj> list1=kkcdtjService.findKKList(kqjl);
//			if(list1!=null){
//				map.put("kuangkelist",list1);
//				map.put("kuangkenum",list1.size());
//			}
//			List<Kkcdtj> list2=kkcdtjService.findCDList(kqjl);
//			if(list2!=null){
//				map.put("chidaolist",list2);
//				map.put("chidaonum",list2.size());
//			}
//			return this.renderString(response,ServerResponse.createBySuccess("获取列表成功",map));
//		}
		return this.renderString(response, ServerResponse.createByErrorMessage("获取信息失败"));
	}
}